﻿using Abp.AutoMapper;
using Abp.Modules;
using Abp.Reflection.Extensions;
using FwProject.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting;
using System;
using Abp.RemoteEventBus.Redis;
using Abp.RemoteEventBus;
using Abp.RemoteEventBus.RabbitMQ;
using Abp.Dapper;
using DapperExtensions;
using System.Collections.Generic;
using System.Reflection;
using Abp.EntityFrameworkCore;
using Castle.MicroKernel.Registration;
using Abp.Runtime.Caching.Redis;
using Castle.Windsor;
using Abp.Dependency;
using Abp.Configuration.Startup;
using FwProject.Comm;
using FwProject.ShopApplication.Configuration;
using Abp.ElasticSearch;
using Abp.ElasticSearch.Configuration;
using FwProject.CatchUntil;

namespace FwProject.ShopApplication
{
       [DependsOn(
        typeof(FwProjectCoreModule),
        typeof(AbpAutoMapperModule),
        typeof(AbpRemoteEventBusRabbitMQModule),
        typeof(AbpEntityFrameworkCoreModule),
        typeof(AbpRedisCacheModule),
        typeof(AbpDapperModule),
        typeof(AbpElasticSearchModule),
        typeof(FwProjectPublicServiceModule))]
    public class FwProjectShopApplicationModule : AbpModule
    {


        private readonly IHostingEnvironment _env;
        private readonly IConfigurationRoot _appConfiguration;

        public FwProjectShopApplicationModule(IHostingEnvironment env)
        {
            _env = env;
            _appConfiguration = env.GetAppConfiguration();

        }

        public override void PreInitialize()
        {
            Configuration.Authorization.Providers.Add<FwProjectAuthorizationProvider>();
            /// <param>缓存配置</param>
            Configuration.Caching.ConfigureAll(cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2);
            });
            Configuration.Caching.AbpConfiguration.ReplaceService<IRedisCacheSerializer, RedisCatch>(DependencyLifeStyle.Transient);
            Configuration.Modules.ElasticSearch().ConnectionString = _appConfiguration["ElasticSearch:ConnectionString"];
            Configuration.Modules.ElasticSearch().AuthUserName = _appConfiguration["ElasticSearch:AuthUserName"];
            Configuration.Modules.ElasticSearch().AuthPassWord = _appConfiguration["ElasticSearch:AuthPassWord"];
            Logger.Info("加载搜索引擎");
        }
        public override void PostInitialize()
        {

            try
            {
                //Configuration.Modules.RemoteEventBus().UseRedis().Configure((setting) =>
                //{
                //    setting.Server = _appConfiguration["JPushmessage:JPushmessageModel:Exception"];
                //});
                Logger.Info("加载消息队列");

                Configuration.Modules.RemoteEventBus().UseRabbitMQ().Configure(setting =>
                {
                                    // setting.Url = _appConfiguration["JPushmessage:JPushmessageModel:localServer"];
#if DEBUG
                                    setting.Url = _appConfiguration["JPushmessage:JPushmessageModel:localServer"];
#else
                                    setting.Url = _appConfiguration["JPushmessage:JPushmessageModel:Server"];
#endif
                                    // "amqp://guest:guest@127.0.0.1:5672/";
                                });

            }
            catch (Exception e)
            {
                Logger.Error("mq 失败采用redis 替代",e);
                Logger.Info("mq 失败采用redis 替代");
                Configuration.Modules.RemoteEventBus().UseRedis().Configure((setting) =>
                {
                    setting.Server = _appConfiguration["JPushmessage:JPushmessageModel:Exception"];
                });
            }

            // enable auto subscribe
            // will scan class which use RemoteEventHandlerAttribute and auto subscribe topic base the attribute info
            // enable auto subscribe
            // will scan class which use RemoteEventHandlerAttribute and auto subscribe topic base the attribute info
            Configuration.Modules.RemoteEventBus().AutoSubscribe();


            //            Configuration.Modules.RemoteEventBus().UseRedis().Configure((setting) =>
            //            {
            //                setting.Server = "127.0.0.1:6379";
            //#if DEBUG
            //                setting.Server = _appConfiguration["JPushmessage:JPushmessageModel:localServer"];

            //#else
            //                setting.Server = _appConfiguration["JPushmessage:JPushmessageModel:Server"];
            //#endif
            //            });


        }


        public override void Initialize()
        {
            var thisAssembly = typeof(FwProjectShopApplicationModule).GetAssembly();

            IocManager.RegisterAssemblyByConvention(thisAssembly);
           
          
        }

     
    }
}
